package com.itheima.service.impl;

import com.itheima.dto.MenuDTO;
import com.itheima.entity.Menu;
import com.itheima.entity.MenuChildren;
import com.itheima.mapper.SysMenuMapper;
import com.itheima.service.SysMenuService;
import com.itheima.vo.MenuChildrenVO;
import com.itheima.vo.MenuVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: kafka
 * @package: com.itheima.service.impl
 * @className: SysMenuServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2023/9/21 14:28
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {
    private final SysMenuMapper sysMenuMapper;

    @Override
    public List<MenuChildrenVO> getAllMenusData() {
        int pid = 0;
        List<MenuChildren> menuChildrenFirst = sysMenuMapper.selectByPid(pid);

        for (MenuChildren menuChildren1 : menuChildrenFirst) {
            List<MenuChildren> menuChildrenSecond = sysMenuMapper.selectByPid(menuChildren1.getId());
            for (MenuChildren menuChildren2 : menuChildrenSecond) {
                List<MenuChildren> menuChildrenThird = sysMenuMapper.selectByPid(menuChildren2.getId());
                menuChildren2.setChildren(menuChildrenThird);
            }
            menuChildren1.setChildren(menuChildrenSecond);
        }

        // 进行转换格式
        List<MenuChildrenVO> menuChildrenVOList3 = new ArrayList<>();
        for (MenuChildren menuChildren1 : menuChildrenFirst) {
            MenuChildrenVO menuChildrenVO2 = new MenuChildrenVO();

            List<MenuChildrenVO> menuChildrenVOList2 = new ArrayList<>();
            List<MenuChildren> menuChildrenSecond = menuChildren1.getChildren();
            for (MenuChildren menuChildren2 : menuChildrenSecond) {
                MenuChildrenVO menuChildrenVO1 = new MenuChildrenVO();

                List<MenuChildrenVO> menuChildrenVOList1 = new ArrayList<>();
                List<MenuChildren> menuChildrenThird = menuChildren2.getChildren();
                for (MenuChildren menuChildren3 : menuChildrenThird) {
                    MenuChildrenVO menuChildrenVO = new MenuChildrenVO();

                    BeanUtils.copyProperties(menuChildren3, menuChildrenVO);
                    menuChildrenVO.setIs_show(menuChildren3.getIsShow());
                    menuChildrenVO.setIs_cache(menuChildren3.getIsCache());
                    menuChildrenVO.setIs_link(menuChildren3.getIsLink());
                    menuChildrenVO.setDel_flag(menuChildren3.getDelFlag());
                    menuChildrenVO.setChildren(null);
                    menuChildrenVOList1.add(menuChildrenVO);
                }

                BeanUtils.copyProperties(menuChildren2, menuChildrenVO1);
                menuChildrenVO1.setIs_show(menuChildren2.getIsShow());
                menuChildrenVO1.setIs_cache(menuChildren2.getIsCache());
                menuChildrenVO1.setIs_link(menuChildren2.getIsLink());
                menuChildrenVO1.setDel_flag(menuChildren2.getDelFlag());
                menuChildrenVO1.setChildren(menuChildrenVOList1);
                menuChildrenVOList2.add(menuChildrenVO1);
            }

            BeanUtils.copyProperties(menuChildren1, menuChildrenVO2);
            menuChildrenVO2.setIs_show(menuChildren1.getIsShow());
            menuChildrenVO2.setIs_cache(menuChildren1.getIsCache());
            menuChildrenVO2.setIs_link(menuChildren1.getIsLink());
            menuChildrenVO2.setDel_flag(menuChildren1.getDelFlag());
            menuChildrenVO2.setChildren(menuChildrenVOList2);
            menuChildrenVOList3.add(menuChildrenVO2);
        }
        return menuChildrenVOList3;
    }

    @Override
    public void addMenu(MenuDTO menuDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDTO, menu);
        menu.setIsShow(menuDTO.getIs_show());
        menu.setIsCache(menuDTO.getIs_cache());
        menu.setIsLink(menuDTO.getIs_link());
        sysMenuMapper.addMenu(menu);
    }

    @Override
    public void deleteMenu(Integer id) {
        String defaultData = sysMenuMapper.selectdeFaultDataById(id);
        if (!Objects.isNull(defaultData)) {
            throw new RuntimeException("原始数据不能删除!");
        }
        List<Integer> ids = sysMenuMapper.selectById(id);   // 通过id找是否有 孩子pid=id
        if (!Objects.isNull(ids)) {       // 说明有孩子
            for (Integer integer : ids) {   // 对孩子进行遍历
                List<Integer> integers = sysMenuMapper.selectById(integer); //再次判断
                if (!Objects.isNull(integers)) {  // 说明还有孩子
                    for (Integer integerMin : integers) {
                        sysMenuMapper.deleteMenu(integerMin);
                    }
                }
                sysMenuMapper.deleteMenu(integer);
            }
        }
        sysMenuMapper.deleteMenu(id);
    }

    @Override
    public void updateMenu(MenuDTO menuDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuDTO,menu);
        menu.setIsShow(menuDTO.getIs_show());
        menu.setIsCache(menuDTO.getIs_cache());
        menu.setIsLink(menuDTO.getIs_link());
        sysMenuMapper.updateMenu(menu);
    }

    @Override
    public MenuVO getMenuData(Integer id) {
        Menu menu = sysMenuMapper.selectMenuById(id);
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(menu,menuVO);
        menuVO.setIs_show(menu.getIsShow());
        menuVO.setIs_cache(menu.getIsCache());
        menuVO.setIs_link(menu.getIsLink());
        menuVO.setDel_flag(menu.getDelFlag());
        return menuVO;
    }
}
